package org.springside.examples.miniweb.service.customer;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.examples.miniweb.dao.customer.CustomerDao;
import org.springside.examples.miniweb.entity.customer.Customer;

/**
 * 客户相关实体的管理类.
 * 
 * @author Bob
 */
@Component
@Transactional(readOnly = true)
public class CustomerManager {

	private static Logger logger = LoggerFactory
			.getLogger(CustomerManager.class);

	private CustomerDao customerDao;

	@Transactional(readOnly = false)
	public void saveCustomer(Customer entity) {
		customerDao.save(entity);
	}

	@Transactional(readOnly = false)
	public Customer getCustomerById (Long id){
		return customerDao.findById(id);
	}
	/**
	 * 删除用户,如果尝试删除超级管理员将抛出异常.
	 */
	@Transactional(readOnly = false)
	public void deleteCustomer(Long id) {
		customerDao.delete(id);
	}

	public List<Customer> getAllCustomer() {
		return (List<Customer>) customerDao.findAll(new Sort(Direction.ASC,
				"id"));
	}

	@Transactional(readOnly = false)
	public Customer findByCustomerName(String customerName) {
		return customerDao.findByName(customerName);
	}
	
	@Transactional(readOnly = false)
	public List<Customer> findByCustomerNameLike(String customerName) {
		return customerDao.findByNameLike(customerName);
	}

	@Autowired
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
}
